package access

import (
	"jtdevice/configs"

	"github.com/dontls/xproto"
)

var (
	s *xproto.Server = nil
)

// Start 启动
func Run() (err error) {
	if s, err = xproto.NewServer(&xproto.Options{
		RequestTimeout: 50,
		RecvTimeout:    30,
		Port:           configs.GViper.GetUint16("access.port"),
		Host:           configs.GViper.GetString("host"),
	}); err != nil {
		return
	}
	s.Handler.Access = AccessHandler
	s.Handler.Dropped = DroppedHandler
	s.Handler.Status = StatusHandler
	s.Handler.Alarm = AlarmHandler
	s.Handler.Event = EventHandler
	s.Handler.Frame = xproto.LogFrame
	go s.ListenTCPAndServe()
	return nil
}

// Stop 停止
func Shutdown() {
	if s != nil {
		s.Release()
	}
}
